package com.oa.ylms.apps.business.assess.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oa.ylms.apps.business.assess.IAssessCheckService;
import com.oa.ylms.apps.business.assess.IAssessInfoService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.ICheckStates;
import com.oa.ylms.apps.constants.assess.IAssessInfoStates;
import com.oa.ylms.apps.dao.assess.IAssessCheckDao;
import com.oa.ylms.apps.dao.assess.IAssessInfoDao;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.assess.AssessCheckValue;
import com.oa.ylms.apps.entity.assess.AssessInfoValue;
import com.oa.ylms.apps.entity.assess.AssessModelValue;

public class AssessCheckServiceImpl implements IAssessCheckService {

	private AssessModelValue assessModel;
	private AssessInfoValue assessInfo;
	private AssessCheckValue assessCheck;
	private IAssessCheckDao dao;
	private IAssessInfoDao assessInfoDao;
	private IAssessInfoService assessInfoService;
	private ICheckPlanService checkPlanService;

	public void checkFailed(StaffInfoValue staffInfo, int assessId,
			String comments) throws Exception {
		assessInfo = assessInfoService.queryAssessInfoByAssessId(assessId);
		int currentState = assessInfo.getStates();
		assessInfo.setStates(IAssessInfoStates.FAILED);
		assessInfoDao.editAssessInfo(assessInfo);
		assessCheck.setStaffInfo(staffInfo);
		assessCheck.setComments(comments);
		assessCheck.setIsPassed(ICheckStates.CHECKFAILED);
		assessCheck.setCheckTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		assessCheck.setAssessInfo(assessInfo);
		assessCheck.setCheckOrder(currentState);
		dao.add(assessCheck);

	}

	public void checkPassed(StaffInfoValue staffInfo, int assessId,
			String comments) throws Exception {
		assessInfo = assessInfoService.queryAssessInfoByAssessId(assessId);
		String planOrders = assessInfo.getCheckPlan().getPlanOrders();
		int currentState = assessInfo.getStates();
		assessInfo.setStates(checkPlanService.getStates(planOrders,
				currentState));
		assessInfoDao.editAssessInfo(assessInfo);
		assessCheck.setStaffInfo(staffInfo);
		assessCheck.setComments(comments);
		assessCheck.setIsPassed(ICheckStates.CHECKPASSED);
		assessCheck.setCheckTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		assessCheck.setAssessInfo(assessInfo);
		assessCheck.setCheckOrder(currentState);
		dao.add(assessCheck);

	}

	public int countTpagesByCheckList(StaffInfoValue staffInfo, int yearNo,
			int monthNo, String modelName, String assessName, int rows)
			throws Exception {
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		if (StringUtils.isNotEmpty(modelName)
				&& StringUtils.isNotBlank(modelName)) {
			assessModel.setModelName(modelName);
			assessModel.setStates(IAssessInfoStates.UNDEFINED);
		}
		if (StringUtils.isNotEmpty(assessName)
				&& StringUtils.isNotBlank(assessName)) {
			assessInfo.setAssessName(assessName);
		}
		assessInfo.setStates(sectionId);
		return dao.countTpagesByCheckList(staffInfo, yearNo, monthNo,
				assessInfo, assessModel, rows);
	}

	public List<AssessInfoValue> queryCheckList(StaffInfoValue staffInfo,
			int yearNo, int monthNo, String modelName, String assessName,
			int rows, int pageNo) throws Exception {
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		if (StringUtils.isNotEmpty(modelName)
				&& StringUtils.isNotBlank(modelName)) {
			assessModel.setModelName(modelName);
			assessModel.setStates(IAssessInfoStates.UNDEFINED);
		}
		if (StringUtils.isNotEmpty(assessName)
				&& StringUtils.isNotBlank(assessName)) {
			assessInfo.setAssessName(assessName);
		}
		assessInfo.setStates(sectionId);
		return dao.queryCheckList(staffInfo, yearNo, monthNo, assessInfo,
				assessModel, rows, pageNo);
	}

	public List<AssessCheckValue> queryAssessCheckByAssessId(int assessId)
			throws Exception {
		return dao.queryAssessCheckByAssessId(assessId);
	}

	public int getTotalNumber() throws Exception {

		return dao.getTotalNumber();
	}

	public AssessModelValue getAssessModel() {
		return assessModel;
	}

	public void setAssessModel(AssessModelValue assessModel) {
		this.assessModel = assessModel;
	}

	public AssessInfoValue getAssessInfo() {
		return assessInfo;
	}

	public void setAssessInfo(AssessInfoValue assessInfo) {
		this.assessInfo = assessInfo;
	}

	public AssessCheckValue getAssessCheck() {
		return assessCheck;
	}

	public void setAssessCheck(AssessCheckValue assessCheck) {
		this.assessCheck = assessCheck;
	}

	public IAssessCheckDao getDao() {
		return dao;
	}

	public void setDao(IAssessCheckDao dao) {
		this.dao = dao;
	}

	public IAssessInfoDao getAssessInfoDao() {
		return assessInfoDao;
	}

	public void setAssessInfoDao(IAssessInfoDao assessInfoDao) {
		this.assessInfoDao = assessInfoDao;
	}

	public IAssessInfoService getAssessInfoService() {
		return assessInfoService;
	}

	public void setAssessInfoService(IAssessInfoService assessInfoService) {
		this.assessInfoService = assessInfoService;
	}

	public ICheckPlanService getCheckPlanService() {
		return checkPlanService;
	}

	public void setCheckPlanService(ICheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

}
